﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Dropthings.DataAccess;
using System.Web.Security;
using System.Configuration;
using System.Globalization;

namespace Dropthings.Business.Activities.UserAccountActivities
{
    public partial class GetRoleTemplateActivity : System.Workflow.ComponentModel.Activity
	{
        private static DependencyProperty UserGuidProperty = DependencyProperty.Register("UserGuid", typeof(Guid), typeof(GetRoleTemplateActivity));

        [ValidationOptionAttribute(ValidationOption.Required)]
        public Guid UserGuid
        {
            get { return (Guid)base.GetValue(UserGuidProperty); }
            set { base.SetValue(UserGuidProperty, value); }
        }

        private static DependencyProperty RoleTemplateProperty = DependencyProperty.Register("RoleTemplate", typeof(RoleTemplate), typeof(GetRoleTemplateActivity));

        public RoleTemplate RoleTemplate
        {
            get { return (RoleTemplate)base.GetValue(RoleTemplateProperty); }
            set { base.SetValue(RoleTemplateProperty, value); }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            //will return the highest priority role template
            var template = DatabaseHelper.GetList<RoleTemplate, Guid>(DatabaseHelper.SubsystemEnum.User,
                    this.UserGuid, LinqQueries.CompiledQuery_GetRoleTemplatesByUserId).FirstOrDefault();

            if (template == null)
            {
                UserSettingTemplateSettingsSection settings = (UserSettingTemplateSettingsSection)ConfigurationManager.GetSection(UserSettingTemplateSettingsSection.SectionName);
                UserSettingTemplateElement anonUserSettingTemplate = settings.UserSettingTemplates[UserSettingTemplateSettingsSection.AnonTemplateKey];
                //as template is null system will look for guest template
                template = DatabaseHelper.GetSingle<RoleTemplate, string>(DatabaseHelper.SubsystemEnum.User, anonUserSettingTemplate.UserName,
                    LinqQueries.CompiledQuery_GetRoleTemplateByTemplateUserName);
            }

            this.RoleTemplate = template;

            return ActivityExecutionStatus.Closed;
        }

        public GetRoleTemplateActivity()
		{
			InitializeComponent();
		}
	}
}
